package com.bolt.support.spring.jpa.jpql;


import com.bolt.support.query.conditions.domain.SqlClause;
import com.bolt.convention.data.Record;
import com.bolt.support.spring.jpa.entity.PersistableEntity;

import javax.inject.Provider;
import javax.persistence.EntityManager;

public class JPAQueryFactory {
    private Provider <EntityManager> entityManager;

    public JPAQueryFactory(final EntityManager entityManager) {
        this.entityManager = new Provider <EntityManager>() {
            @Override
            public EntityManager get() {
                return entityManager;
            }
        };
    }

    public JPAQuery <?> query() {
        return new JPAQuery <Void>(entityManager.get());
    }

    public <T> JPAQuery <T> select(Class <T> type, SqlClause... exprs) {
        return query().select(type, exprs);
    }

    public <T> JPAQuery <T> selectAll(Class <T> type, SqlClause... exprs) {
        return query().selectAll(type, exprs);
    }

    public <T> JPAQuery <T> selectDistinct(Class <T> type, SqlClause... exprs) {
        return query().selectDistinct(type, exprs);
    }

    public JPAQuery<Record>  select(SqlClause... exprs) {
        return  query().select(exprs);
    }

    public JPAQuery<Record>  selectDistinct( SqlClause... exprs) {
        return query().selectDistinct( exprs);
    }

    public <T  extends PersistableEntity> JPAQuery <T> from(Class<T> entity) {
        return query().from(entity);
    }

    public JPAUpdate update(Class <? extends PersistableEntity> entity) {
        return new JPAUpdate(entityManager.get(), Clauses.of(entity));
    }

    public JPADelete delete(Class <? extends PersistableEntity> entity) {
        return new JPADelete(entityManager.get(), Clauses.of(entity));
    }
}

 